package com.app.bimo.library_db.helper;


import com.app.bimo.commonbusiness.util.DataUtil;
import com.app.bimo.library_db.BookData;
import com.app.bimo.library_db.dao.BookDataDao;
import com.app.bimo.library_db.dao.DaoSession;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

public class BookHelper {

    private static volatile BookHelper sInstance;
    private static DaoSession daoSession;
    private static BookDataDao bookDataDao;

    public static BookHelper getsInstance() {
        if (sInstance == null) {
            synchronized (BookHelper.class) {
                if (sInstance == null) {
                    sInstance = new BookHelper();
                    daoSession = DaoDbHelper.getInstance().getSession();
                    bookDataDao = daoSession.getBookDataDao();

                }
            }
        }
        return sInstance;
    }

    /**
     * 保存书籍
     * 保存或更新
     */
    public void saveBookToBookRack(BookData bookData) {
        bookData.setInBookself(1);
        bookDataDao.insertOrReplace(bookData);
    }

    /**
     * 保存书籍
     * 保存或更新
     */
    public void saveBookToDownLoad(List<BookData> books) {
        for (int i = 0; i < books.size(); i++) {
            books.get(i).setInDownLoad(1);
        }
        bookDataDao.insertOrReplaceInTx(books);
    }

    /**
     * 保存书籍
     * 保存或更新
     */
    public void saveBookToDownLoad(BookData book) {
        bookDataDao.insertOrReplaceInTx(book);

    }

    /**
     * 保存书籍
     * 保存或更新
     */
    public void saveBookReadRecord(BookData bookData) {
        bookData.setInReadRecord(1);
        bookDataDao.insertOrReplace(bookData);
    }

//    /**
//     * 保存书籍
//     * 保存或更新   有这本书 更新就好  没有这本书 执行插入
//     */
//    public void saveBooksToBookRack(List<BookData> books, String uid) {
//        for (int i = 0; i < books.size(); i++) {
//            BookData data = books.get(i);
//            BookData localData = findBookByUserIdAndNovelId(uid, data.getNovelid());
//            if (localData != null) {
//                Log.e("aaa", "localData: " + localData.toString());
//            }

//            if (isNeedUpData(uid, data.getNovelid())) {
//                data.setInBookself(1);
//                data.setOrderTime(i);
//                data.setInDownLoad(localData.getInDownLoad());
//                data.setInReadRecord(localData.getInReadRecord());
//                bookDataDao.update(data);
//            } else {
//                data.setInBookself(1);
//                data.setUuid(uid);
//                data.setOrderTime(i);
//                bookDataDao.insertOrReplaceInTx(data);
//            }
//
//        }
//
//
//    }

    /**
     * 保存书籍
     * 保存或更新   有这本书 更新就好  没有这本书 执行插入
     */
    public void saveBooksToBookRack(List<BookData> books) {
        bookDataDao.insertOrReplaceInTx(books);
        List<BookData> list = bookDataDao.queryBuilder().list();


    }


    private boolean isNeedUpData(String uid, String novelId) {
        BookData data = bookDataDao.queryBuilder().where(BookDataDao.Properties.Uuid.eq(uid), BookDataDao.Properties.Novelid.eq(novelId)).unique();
        if (data == null) {
            return false;
        } else {
            if (data.getInBookself() == 1 || data.getInDownLoad() == 1 || data.getInReadRecord() == 1) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 从书架删除书籍
     */
    public void delBooksToBookrack(List<BookData> books) {
        List<BookData> removeBooks = books;
        List<BookData> updataBooks = new ArrayList<>();
        for (int i = 0; i < books.size(); i++) {
            BookData bookData = books.get(i);
            bookData.setInBookself(0);
            if (!isNeedClear(books.get(i))) {
                removeBooks.remove(bookData);
                updataBooks.add(bookData);
            }
        }
        if (removeBooks.size() > 0) {
            bookDataDao.deleteInTx(removeBooks);
        }
        if (updataBooks.size() > 0) {
            bookDataDao.insertOrReplaceInTx(updataBooks);
        }

    }

    /**
     * 从下载书籍中删除书籍
     */
    public void delBooksToDownLoad(List<BookData> books) {
        List<BookData> removeBooks = books;
        List<BookData> updataBooks = new ArrayList<>();
        for (int i = 0; i < books.size(); i++) {
            BookData bookData = books.get(i);
            bookData.setInDownLoad(0);
            if (!isNeedClear(books.get(i))) {
                removeBooks.remove(bookData);
                updataBooks.add(bookData);
            }
        }
        if (removeBooks.size() > 0) {
            bookDataDao.deleteInTx(removeBooks);
        }
        if (updataBooks.size() > 0) {
            bookDataDao.insertOrReplaceInTx(updataBooks);
        }

    }

    /**
     * 判断是否可以删除  还是只是移除状态
     *
     * @param book
     * @return
     */
    private boolean isNeedClear(BookData book) {
        return (book.getInBookself() == 0) && (book.getInDownLoad() == 0) && (book.getInReadRecord() == 0);
    }


    /**
     * 根据小说id 查询
     *
     * @return
     */
    public List<BookData> findBookByNovelId(String novelid) {
        return bookDataDao.queryBuilder().where(BookDataDao.Properties.Novelid.eq(novelid)).list();
    }

    /**
     * 根据小说id 查询
     *
     * @return
     */
    public BookData findBookByUserIdAndNovelId(String uuid, String novelid) {
        if (DataUtil.isEmpty(uuid)) {
            return null;
        }
        return bookDataDao.queryBuilder().where(BookDataDao.Properties.Uuid.eq(uuid), BookDataDao.Properties.Novelid.eq(novelid)).unique();
    }

    /**
     * 获取书架列表
     *
     * @return
     */
    public List<BookData> findBookrackBooks(String uuid) {
        return bookDataDao.queryBuilder().where(BookDataDao.Properties.Uuid.eq(uuid), BookDataDao.Properties.InBookself.eq(1)).orderAsc(BookDataDao.Properties.OrderTime).list();
    }

    /**
     * 获取我的下载列表
     *
     * @return
     */
    public List<BookData> findMineDownLoadBooks(String uuid) {
        return bookDataDao.queryBuilder().where(BookDataDao.Properties.Uuid.eq(uuid), BookDataDao.Properties.InDownLoad.eq(1)).list();
    }

    /**
     * 获取我的阅读记录列表
     *
     * @return
     */
    public BookData findReadRecordBooks(String uuid, String Novelid) {
        return bookDataDao.queryBuilder().where(BookDataDao.Properties.Uuid.eq(uuid), BookDataDao.Properties.InReadRecord.eq(1), BookDataDao.Properties.Novelid.eq(Novelid)).unique();
    }


    public Observable<List<BookData>> localObservable(String uuid) {
        if (DataUtil.isEmpty(uuid)) {
            return Observable.just(new ArrayList<BookData>());
        }
        return Observable.just(findBookrackBooks(uuid));
    }

    public Observable<List<BookData>> localDownLoadObservable(String uuid) {
        if (DataUtil.isEmpty(uuid)) {
            return Observable.just(new ArrayList<BookData>());
        }
        return Observable.just(findMineDownLoadBooks(uuid));
    }

    /**
     * 查询数据库里所有书籍信息
     *
     * @return
     */
    public List<BookData> findAllBooks() {
        return bookDataDao.queryBuilder().list();
    }

}
